package com.hibuy.pc.controller;

import com.alibaba.fastjson.JSONArray;
import com.hibuy.pc.model.shiro.User;
import com.hibuy.pc.service.shiro.UserService;
import com.hibuy.pc.util.GsonUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by admin on 2017/12/28.
 */
@Controller
@RequestMapping("/member")
public class UserController {

    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/list",method = RequestMethod.GET)
//    public String toUserList(HttpServletRequest request){
//        List<User> userList = userService.findUserList();
//        request.setAttribute("users",JSONArray.toJSON(userList));
//        return "member/list";
//    }
//
//
//    @RequestMapping(value = "/list",method = RequestMethod.POST)
//    @ResponseBody
//    public List<User> getUserList(){
//        return userService.findUserList();
//    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String toUserList(HttpServletRequest request){
        List<User> userList = userService.findUserList();
        request.setAttribute("users",userList);
        return "member/list";
    }


    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addUser (HttpServletRequest request){

        String nickname = request.getParameter("nickname");
        String email = request.getParameter("email");
        String pswd = request.getParameter("pswd");
        Integer status = Integer.valueOf(request.getParameter("status"));
        Integer roleId = Integer.valueOf(request.getParameter("roleId"));
        User user = new User();
        user.setNickname(nickname);
        user.setEmail(email);
        user.setPswd(pswd);
        user.setStatus(status);
        userService.addUser(user,roleId);
        return "";
    }



}
